
<?php
if(isset($_SESSION['QuyenHan']) && $_SESSION['QuyenHan'] == 4)
{
	$trangthai = new trangthaibs();
	$dstrangthai = $trangthai->GetList();
	
	$vanbandi = new vanbandibs();
	
	if(isset($_POST['btnTim']))
	{
		
		$SovaKyHieu = $_POST['SovaKyHieu'];
		$NoiBanHanh = $_POST['NoiBanHanh'];
		$NgayBanHanh = str_replace('-',',',$_POST['NgayBanHanh']);
		$NgayHetHan = $_POST['TrichYeu'];
		
		
		$vanbandi->SovaKyHieu = $SovaKyHieu;
		$vanbandi->NgayBanHanh = $NgayBanHanh;
		$vanbandi->TrichYeu = $TrichYeu;
		$vanbandi->NoiBanHanh = $NoiBanHanh;
	
	}
	if(isset($_GET['mtt']))
	{
		$vanbandi->MaTrangThai = $_GET['mtt'];
		$mtt=$_GET['mtt'];
	}else
	$mtt=0;
	$dsvb = $vanbandi->GetList();
	$tongsodong =  count($dsvb);
	if(!isset($_GET['start']))
		$start = 0;
	else
		$start = $_GET['start'];
	
	$limit = 15;
	$next = $start + $limit;
	$back = $start - $limit;
	$danhsach = $vanbandi->PageList($start, $limit);
	$nguoidung = new nguoidungbs();
	$dsnguoidung = $nguoidung->GetList();
	
?>

<div id="NoiDung" class="form">
  <h3  class="text-primary">VĂN BẢN ĐI</h3>
  <div style="position:relative; left:840px; top: 20px;"><a href="#"  style="margin-left:10px"><span id="clickSearch" class="glyphicon glyphicon-search" style="font-size:18px"></span></a></div>
  <div style="margin-top:20px; display:none" id="search">
    <form  action="vanbandi" method="post" role="form">
      <table class="table borderless">
        <tbody>
          <tr>
           <td>
                <div class="form-group form-custom">
                  <label  for="sovakyhieu">Số và ký hiệu:</label>
                    <input type="text" class="form-control" placeholder="Số và ký hiệu" name="SovaKyHieu" class="required" />
                </div>
        	</td>
            <td>
            	  <div class="form-group form-custom">
                  <label  for="trichyeu">Trích yếu:</label>
                    <input type="text" class="form-control" placeholder="Trích yếu" name="TrichYeu" class="required" />
                  
                </div>
           </td>
            
         </tr>
         <tr>
         	<td>
            	  <div class="form-group form-custom">
                  <label  for="noibanhanh">Nơi ban hành:</label>
                    <input type="text" class="form-control" placeholder="Nơi ban hành" name="NoiBanHanh" class="required" />
                  
                </div>
           </td>
          	<td>
              <div class=" form-group form-custom control-group">
                <label class="control-label">Ngày ban hành</label>
                <div class="controls input-append date form_date" data-date="" data-date-format="dd MM yyyy" data-link-field="dtp_input2" data-link-format="yyyy-mm-dd">
                    <input size="16" type="text" value="" readonly="readonly" name="NgayBanHanh" class="form-control">
                    <span class="add-on"><i class="icon-remove"></i></span>
					<span class="add-on"><i class="icon-th"></i></span>
                </div>
				
            </div>
            
        	</td>
        </tr>
        <tr>
        <td colspan="2">
        <input type="submit" value="Tìm" class="btn btn-primary btn-primary pull-right" name="btnTim" />
         </td>
        </tr>
       </tbody>
      </table>
    </form>
  </div>
 <script>
	$("#clickSearch").click(function()
	{
		$("#search").toggle("fast");
	})
 </script> 
 <div class="btnTrangThai">
 		
       
        <a href="vanbandi_<?php echo $dstrangthai[4]['MaTrangThai']?>" >
         <label class= 'label label-success'><?php echo $dstrangthai[4]['TenTrangThai'] ?> </label>
        </a>
        
        <a href="vanbandi_<?php echo $dstrangthai[5]['MaTrangThai']?>" >
         <label class= 'label label-info'><?php echo $dstrangthai[5]['TenTrangThai'] ?> </label>
        </a>

        <a href="vanbandi" >
         <label class= 'label label-primary'>Tất cả </label>
        </a>
      
  </div>
 
  <!--Bang van ban den-->
  
  <div style="margin-top:20px;">
 <form action="vanbandi" method="post">
    <table class="table table-hover table-bordered table-striped">
      <thead>
        <tr>
          <th style="text-align:center"> STT </th>
         
          <th  style="text-align:center"> Số và ký hiệu </th>

          <th style="text-align:center"> Ngày ban hành </th>
          <th  style="text-align:center"> Trích yếu </th>
          <th style="text-align:center"> Nơi ban hành</th>
          <?php if(isset($_GET['mtt']) && $_GET['mtt'] == 5)
			{
			?>
          <th style="text-align:center"> Giám đốc </th>
          <th style="text-align:center"> <span class="glyphicon glyphicon-ok"></span>
          <?php } ?>
         
        </tr>
      </thead>
      <tbody>
        <?php
		
	if(!is_null($danhsach[0]['MaVanBanDi']))
	{
		
				for($i = 0; $i < count($danhsach); $i++)
				{
					$stt=$i+1;
					
					
					if($danhsach[$i]['MaTrangThai'] == 6)
					echo "<tr class='info'>";
					elseif($danhsach[$i]['MaTrangThai'] == 5)
					echo "<tr class='success'>";
						
						echo "<td>".$stt."</td>";
						
						if(isset($_POST['SovaKyHieu']))
							echo "<td> <a href='vanbandi-chitiet_{$danhsach[$i]['MaVanBanDi']}'>".highlight($danhsach[$i]['SovaKyHieu'], $SovaKyHieu)."</a></td>";
						else
							echo "<td> <a href='vanbandi-chitiet_{$danhsach[$i]['MaVanBanDi']}'>{$danhsach[$i]['SovaKyHieu']} </a></td>";
						
					
						
					
						if(isset($_POST['NgayBanHanh']))
							echo "<td>".highlight(InNgay($danhsach[$i]['NgayBanHanh']), str_replace(',','/',$NgayBanHanh) )."</td>";
						else
							echo "<td>".InNgay($danhsach[$i]['NgayBanHanh'])."</td>";
						
						if(isset($_POST['TrichYeu']))
							echo "<td>".highlight($danhsach[$i]['TrichYeu'],$TrichYeu)."</td>";
						else
							echo "<td>{$danhsach[$i]['TrichYeu']}</td>";
						
						if(isset($_POST['NoiBanHanh']))
							echo "<td>".highlight($danhsach[$i]['NoiBanHanh'],$NoiBanHanh)."</td>";
						else
							echo "<td>{$danhsach[$i]['NoiBanHanh']}</td>";
					
						
						
						if(isset($_GET['mtt']) && $_GET['mtt'] == 5 && $danhsach[$i]['MaNguoiDuyet'] == 0)
						{
							echo "<td>
							<select name='MaNguoiDuyet[]'>";
							for($j=0;$j<count($dsnguoidung);$j++)
							{
								if($dsnguoidung[$j]['QuyenHan'] == 1)
								echo "<option value=".$dsnguoidung[$j]['MaNguoiDung'].">{$dsnguoidung[$j]['TenNguoiDung']}</option>";
							}
						echo"	</select> </td>";						
							echo "<td><input type='checkbox' name='checkDuyet[]' value=".$danhsach[$i]['MaVanBanDi']." /></td>";
						}
						
						
					echo "</tr>";
				}
	}
			if(isset($_GET['mtt']) && $_GET['mtt'] == 5)
			{
			?>
           <tr>
           <td colspan="6">
                	<input value="Gởi"  class="btn btn-primary btn-primary pull-right" name="btnGoi" type="submit" />
                </td>
            	<td>
                	<input value="Duyệt"  class="btn btn-primary btn-primary pull-right" name="btnDuyet" type="submit" />
                </td>
            </tr>
            <?php }?>
      </tbody>
    </table>
  </div>
 </form>
 <?php
		if($tongsodong > $limit)
		{
			echo "<div id='PhanTrang' align = 'center'>";
				echo "<ul class='pagination pagination-sm'>";
				if($mtt!=0)
				{
						if($start > 0)
						echo "<li><a href='vanbandi-page-loai_$mtt-$back'>&laquo;</a></li>";
					$j = 1;
					for($i = 0; $i < $tongsodong; $i += $limit)
					{	  
							echo "<li><a href='vanbandi-page-loai_$mtt-$i'>" . $j . "</a></li>";
						$j++;
					}
					if($start + $limit < $tongsodong)
						echo "<li><a href='vanbandi-page-loai_$mtt-$next'>&raquo;</a></li>";
				}else{
					if($start > 0)
						echo "<li><a href='vanbandi-page_$back'>&laquo;</a></li>";
					$j = 1;
					for($i = 0; $i < $tongsodong; $i += $limit)
					{	   
							echo "<li><a href='vanbandi-page_$i'>" . $j . "</a></li>";
						$j++;
					}
					if($start + $limit < $tongsodong)
						echo "<li><a href='vanbandi-page_$next'>&raquo;</a></li>";
				}
				echo "</ul>";
			echo "</div>";
			echo "<br/>";
		}
?>
</div>
<script type="text/javascript">
   
	$('.form_date').datetimepicker({
      	format: 'dd-mm-yyyy',
        weekStart: 1,
        todayBtn:  1,
		autoclose: 1,
		todayHighlight: 1,
		startView: 2,
		minView: 2,
		forceParse: 0
    });
	
</script>

<?php
	if(isset($_POST['btnDuyet']))
	{
		$vb = new vanbandibs();
		$vb->MaNguoiDuyet = $_SESSION['MaNguoiDung'];
		$vb->MaTrangThai = 6;
		$MaVanBanDi = $_POST['checkDuyet'];
		for($i=0; $i < count($MaVanBanDi); $i++)
		{
			$vb->MaVanBanDi = $MaVanBanDi[$i];
			$vb->ChangeTrangThai();
		}
		header("Location: vanbandi");
	}elseif(isset($_POST['btnGoi']))
	{
		$vb = new vanbandibs();
		$vb->MaTrangThai = 5;
		$MaNguoiDuyet = $_POST['MaNguoiDuyet'];
		$MaVanBanDi = $_POST['checkDuyet'];
		for($i=0; $i < count($MaVanBanDi); $i++)
		{
			$vb->MaVanBanDi = $MaVanBanDi[$i];
			$vb->MaNguoiDuyet = $MaNguoiDuyet[$i];
			$vb->ChangeTrangThai();
		}
		header("Location: vanbandi");
	}

}else
{
	ThongBaoLoi("Vui lòng đăng nhập !");
}

?>